package docchepfile;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;

public class ReadWriteTestFile {
	public static void main(String[] args) throws Exception{
		ReadFile("C://Users/hao huynh/Desktop/copy.txt");
		WriteFile("C://Users/hao huynh/Desktop/copy.txt", "C://Users/hao huynh/Desktop/out.txt");
	
	}
	public static void ReadFile(String fileName) throws Exception
	{
		FileInputStream fis=new FileInputStream(fileName);
		Scanner in=new Scanner(fis, "UTF-8");
		while(in.hasNextLine()){
			String line=in.nextLine();
			System.out.println(line);
		}
		fis.close();in.close();
	}
	//read by line
	public static void WriteFile(String inputName, String outputName ) throws Exception{
		FileInputStream fis=new FileInputStream(inputName);
		Scanner in=new Scanner(fis, "UTF-8");
		FileOutputStream fos=new FileOutputStream(outputName);
		PrintWriter out=new PrintWriter(fos ,true);
		
		while(in.hasNextLine()){
			String line=in.nextLine();
			out.println(line);
		}
		fis.close();in.close();
		fos.close();out.close();
		System.out.println("copy file success");
	}
	//read by byte
	public static void Copy(String inputName, String outputName) throws Exception{
		FileInputStream fis=new FileInputStream(inputName);
		FileOutputStream fos=new FileOutputStream(outputName);
		
		byte []buff=new byte [1024];
		int len=0;
		while((len=fis.read(buff))!=-1){
			fos.write(buff, 0, len);;
			fos.flush();
		}
		fis.close();fos.close();
		System.out.println("copy file success");
	}
}
